package cn.sc.allinpay.allfavour.business.splash;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;

import cn.pai.mvp.presenter.PaiPresenter;
import cn.sc.allinpay.allfavour.app.Constant;
import cn.sc.allinpay.allfavour.business.common.data.CashierData;
import cn.sc.allinpay.allfavour.business.common.data.ConfigData;
import cn.sc.allinpay.allfavour.business.common.data.PayInfo;
import cn.sc.allinpay.allfavour.business.common.data.PosInfo;
import cn.sc.allinpay.allfavour.business.common.data.SpToken;
import cn.sc.allinpay.allfavour.business.common.data.UserInfo;
import cn.sc.allinpay.allfavour.business.common.utils.SpUtils;
import cn.sc.allinpay.allfavour.business.login.LoginActivity;
import cn.sc.allinpay.allfavour.business.main.MainActivity;

/**
 * 启动 presenter
 * author：pany
 * on 2017/12/1 11:27
 */
public class SplashPresenter extends PaiPresenter<SplashContract.View> implements SplashContract.Presenter {

    private SplashModel model = new SplashModel();
    private boolean isAnimateFinish = false;

    @Override
    public void start() {


    }

    @Override
    public void setAnimateFinish() {
        this.isAnimateFinish = true;
    }

    @Override
    public void startActivity() {
        if (isAnimateFinish) {
            String cashierJson = SpUtils.get(view.getContext(), Constant.SPNAME, Constant.SpKey.CashierData, "");
            if (!TextUtils.isEmpty(cashierJson)) {
                CashierData cashierData = JSON.parseObject(cashierJson, CashierData.class);
                model.getGlobal().setCashierData(cashierData);
            }
            String configJson = SpUtils.get(view.getContext(), Constant.SPNAME, Constant.SpKey.ConfigData, "");
            if (!TextUtils.isEmpty(configJson)) {
                ConfigData configData = JSON.parseObject(configJson, ConfigData.class);
                model.getGlobal().setConfigData(configData);
            }
            String posInfoJson = SpUtils.get(view.getContext(), Constant.SPNAME, Constant.SpKey.PosInfo, "");
            if (!TextUtils.isEmpty(posInfoJson)) {
                PosInfo posInfo = JSON.parseObject(posInfoJson, PosInfo.class);
                model.getGlobal().setPosInfo(posInfo);
            }
            String userInfoJson = SpUtils.get(view.getContext(), Constant.SPNAME, Constant.SpKey.UserIfo, "");
            if (!TextUtils.isEmpty(userInfoJson)) {
                UserInfo userInfo = JSON.parseObject(userInfoJson, UserInfo.class);
                model.getGlobal().setUserInfo(userInfo);
            }
            String payInfoJson = SpUtils.get(view.getContext(), Constant.SPNAME, Constant.SpKey.PayInfo, "");
            if (!TextUtils.isEmpty(payInfoJson)) {
                PayInfo payInfo = JSON.parseObject(payInfoJson, PayInfo.class);
                model.getGlobal().setPayInfo(payInfo);
            }
            String tokenJson = SpUtils.get(view.getContext(), Constant.SPNAME, Constant.SpKey.Token, "");
            if (!TextUtils.isEmpty(tokenJson)) {
                SpToken token = JSON.parseObject(tokenJson, SpToken.class);
                model.getGlobal().setToken(token);
            }
            String serverName = SpUtils.get(view.getContext(), Constant.SPNAME, Constant.SpKey.SERVER_NAME, "");
            if (!TextUtils.isEmpty(serverName)) {
                model.getGlobal().setServer(serverName);
            } else {
                model.getGlobal().setServer("");
            }
            if (!TextUtils.isEmpty(cashierJson) && !TextUtils.isEmpty(userInfoJson) && !TextUtils.isEmpty(tokenJson)) {
                view.toActivity(MainActivity.class, null);
                view.finish();
            } else {
                view.toActivity(LoginActivity.class, null);
                view.finish();
            }
        }
    }
}
